﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Kooboo.HtmlFunc.Function;
using Kooboo.HtmlFunc.Providers;

namespace Kooboo.HtmlFunc.Analysis.Visitors
{
    public class HtmlPartVisitor : StartEndVisitor
    {
        public HtmlPartVisitor(VisitorContext context)
            : base(context, FuncType.HtmlPart)
        {
        }

        protected override object OnQualified(HtmlCommentNode startNode, HtmlCommentNode endNode)
        {
            var ownerPosition = (IPosition)this.Context.Items[FuncType.Position];
            var order = ownerPosition.ChildCount++;

            // add
            var htmlPart = Provider.Factory.GetProvider<IHtmlPart>();
            htmlPart.StartNode = startNode;
            htmlPart.EndNode = endNode;
            htmlPart.Order = order;
            htmlPart.OwnerPosition = ownerPosition;
            this.Context.Result.HtmlParts.Add(htmlPart);

            // ret context mark
            return htmlPart;
        }

        protected override FuncType RequireType
        {
            get
            {
                return FuncType.Position;
            }
        }

        protected override FuncType ExclusiveType
        {
            get
            {
                return this.FuncType
                    | FuncType.ViewPart
                    | FuncType.Label
                    | FuncType.HtmlBlockPart;
            }
        }
    }
}
